package com.example.serverSide.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.serverSide.commom.dto.OrderDto;
import com.example.serverSide.mapper.ClientMapper;
import com.example.serverSide.mapper.OrderDetailMapper;
import com.example.serverSide.mapper.ProductMapper;
import com.example.serverSide.mapper.ProductOrderMapper;
import com.example.serverSide.pojo.Client;
import com.example.serverSide.pojo.OrderDetail;
import com.example.serverSide.pojo.Product;
import com.example.serverSide.pojo.ProductOrder;
import com.example.serverSide.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductOrderServiceImpl extends ServiceImpl<ProductOrderMapper, ProductOrder> implements ProductOrderService {
    @Autowired
    ProductOrderMapper productOrderMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    ClientMapper clientMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Override
    public List<OrderDto> getOrderList() {
        List<OrderDto> list = new ArrayList<OrderDto>();
        LambdaQueryWrapper<ProductOrder> query = new LambdaQueryWrapper<>();
        List<ProductOrder> productOrders = productOrderMapper.selectList(query);
        for (ProductOrder order : productOrders) {
            OrderDto orderDto = new OrderDto();
            orderDto.setId(order.getId());
            orderDto.setPayMethod(order.getPayMethod());
            if (order.getPayMethod() == 1){
                orderDto.setPayMethodName("线下支付");
            }
            if (order.getPayMethod() == 2){
                orderDto.setPayMethodName("线上支付");
            }
            orderDto.setStatus(order.getStatus());
            if (order.getStatus() == 1){
                orderDto.setStatusName("待支付");
            }
            if (order.getStatus() == 2){
                orderDto.setStatusName("运输中");
            }
            if (order.getStatus() == 3){
                orderDto.setStatusName("已完成");
            }
            Client client = clientMapper.selectById(order.getClientId());
            orderDto.setClientName(client.getName());
            LambdaQueryWrapper<OrderDetail> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OrderDetail::getOrderId, order.getId());
            List<OrderDetail> orderDetails = orderDetailMapper.selectList(queryWrapper);
            for (OrderDetail detail : orderDetails){
                Product product = productMapper.selectById(detail.getProductId());
                detail.setProductName(product.getName());
                detail.setImage(product.getImage());
            }
            orderDto.setOrderDetails(orderDetails);
            list.add(orderDto);
        }
        return list;
    }
}
